<?php

namespace App\Admin\Controllers;

use App\Models\MemberLevel;
use App\Models\Store;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class MemberLevelController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '会员等级';
    protected $state = [
        'on' => ['value' => 1, 'text' => '是', 'color' => 'success'],
        'off' => ['value' => 2, 'text' => '否', 'color' => 'danger']
    ];
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new MemberLevel());

//        $grid->column('id', __('Id'));
        $grid->column('store.name', __('Store id'));
        $grid->column('name', __('Name'));
        $grid->column('is_auto_upgrade', __('Is auto upgrade'))->switch($this->state);
        $grid->column('is_opening', __('Is opening'))->switch($this->state);
        $grid->column('amount', __('Amount'))->display(function ($data) {
            return '￥'.$data;
        });
        $grid->column('give_amount', __('Give amount'))->display(function ($data) {
            return '￥'.$data;
        });
        $grid->column('discount', __('Discount'));
        $grid->column('created_at', __('Created at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(MemberLevel::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('store_id', __('Store id'));
        $show->field('name', __('Name'));
        $show->field('is_auto_upgrade', __('Is auto upgrade'));
        $show->field('is_opening', __('Is opening'));
        $show->field('amount', __('Amount'));
        $show->field('give_amount', __('Give amount'));
        $show->field('discount', __('Discount'));
        $show->field('description', __('Description'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('deleted_at', __('Deleted at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new MemberLevel());

        $form->select('store_id', '门店')->options(Store::all()->pluck('name', 'id'))->rules('required');
        $form->text('name', __('Name'))->rules('required|max:20');
        $form->switch('is_auto_upgrade', __('Is auto upgrade'))->states($this->state)->default(1);
        $form->switch('is_opening', __('Is opening'))->states($this->state)->default(1);
        $form->currency('amount', __('Amount'))
            ->symbol('￥')
            ->rules('required|numeric|between:0,99999999999');
        $form->currency('give_amount', __('Give amount'))
            ->symbol('￥')
            ->rules('required|numeric|between:0,99999999999');;
        $form->rate('discount', __('Discount'))
            ->rules('required|numeric|min:0|max:100');
        $form->editor('description', __('Description'));

        return $form;
    }
}
